﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Web.Interfaces;

namespace Cofe.Web.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class BitmapResponseFormatter : IHttpResponseMessageFormatter
    {
        public bool CanCreateResponse(HttpRequestMessage request,
            object returnVal, ParameterDic pd, ResponseFormatterInfo info)
        {
            return (returnVal is BitmapContainer);
        }

        public HttpResponseMessage CreateResponse(HttpRequestMessage request,
            object returnVal, ParameterDic pd, ResponseFormatterInfo info)
        {
            BitmapContainer bc = returnVal as BitmapContainer;
            if (bc == null)
                throw new Exception();


            HttpResponseMessage result = new HttpResponseMessage(info.StatusCode);
            result.Content = new StreamContent(bc.GetStream(info.ResourceAttribute.MediaType).Stream);

            return result;
        }

    }
}
